<%@ page import="com.xpy.utils.ConnSqlServer" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.xpy.entity.Admin" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
%>
<html>
<head>
    <title>管理员维护</title>
    <link rel="stylesheet" href="css/style2.css">
    <style>
        .pagination-info {
            text-align: center;
            margin-top: 20px;
        }
        .pagination-info a {
            margin: 0 5px;
        }
        .admin_operations {
            text-align: center;
        }
        table{
            margin: auto;
        }
        .admin_operations input[type="checkbox"] {
            margin-right: 10px;
        }
        .admin_operations button {
            margin: 3px;
        }
    </style>
    <script>
        function modifyAdmin() {
            var adminCheckboxes = document.querySelectorAll('input[name="selectedAdmin"]:checked');
            if (adminCheckboxes.length === 0) {
                alert("请选择要修改的管理员！");
            } else {
                var selectedId = adminCheckboxes[0].value;
                window.location.href = 'admin_modify.jsp?id=' + selectedId;
            }
        }
    </script>
</head>
<body>
<%@include file="head.jsp"%>
<div class="admin-container">
    <%
        int pages = request.getParameter("page") != null ? Integer.parseInt(request.getParameter("page")) : 1;
        int recordsPerPage = 5;
        int offset = (pages - 1) * recordsPerPage;

        Connection conn = ConnSqlServer.getConnection();
        PreparedStatement pstm = conn.prepareStatement("select * from admin order by id offset ? rows fetch next ? rows only");
        pstm.setInt(1, offset);
        pstm.setInt(2, recordsPerPage);
        ResultSet rs = pstm.executeQuery();

        List<Admin> adminList = new ArrayList<>();
        while (rs.next()) {
            Admin admin = new Admin();
            admin.setId(rs.getInt("id"));
            admin.setName(rs.getString("name"));
            admin.setPower(rs.getInt("power"));
            adminList.add(admin);
        }

        PreparedStatement pstm2 = conn.prepareStatement("select count(*) as total from admin");
        ResultSet rs2 = pstm2.executeQuery();
        rs2.next();
        int totalRecords = rs2.getInt("total");
        int totalPages = (int) Math.ceil((double) totalRecords / recordsPerPage);
    %>
    <table>
        <thead>
        <tr>
            <th>序号</th>
            <th>操作</th>
            <th>管理员姓名</th>
            <th>权限</th>
        </tr>
        </thead>
        <tbody>
        <% int index = (pages - 1) * recordsPerPage + 1; %>
        <% for (Admin admin : adminList) { %>
        <tr>
            <td><%= index %></td>
            <td><input type="checkbox" name="selectedAdmin" value="<%= admin.getId() %>"></td>
            <td><%= admin.getName() %></td>
            <td><%= admin.getPower() == 1 ? "超级管理员" : "普通管理员" %></td>
        </tr>
        <% index++; %>
        <% } %>
        </tbody>
    </table>

    <div class="pagination-info">
        页次：<%= pages %>/<%= totalPages %>
        本页：<%= adminList.size() %>/<%= totalRecords %>
        <% int currentPage = pages; %>
        <% int nextPage = currentPage < totalPages ? currentPage + 1 : totalPages; %>
        <% int prevPage = currentPage > 1 ? currentPage - 1 : 1; %>
        <a href="?page=1">首页</a>
        <a href="?page=<%= prevPage %>">前页</a>
        <a href="?page=<%= nextPage %>">后页</a>
        <a href="?page=<%= totalPages %>">尾页</a>
    </div>
    <div class="admin_operations">
        <input type="checkbox" name="selectAllAdmins">
        <button onclick="window.location.href='admin_new.jsp'">新增管理员</button>
        <button onclick="modifyAdmin()">修改管理员</button>
        <button onclick="deleteAdmin()">删除管理员</button>
    </div>
</div>
<%@include file="bottom.jsp"%>
<script>
    var selectAllAdminsCheckbox = document.querySelector('input[name="selectAllAdmins"]');
    var adminCheckboxes = document.querySelectorAll('input[name="selectedAdmin"]');

    selectAllAdminsCheckbox.addEventListener('click', function() {
        for (var i = 0; i < adminCheckboxes.length; i++) {
            adminCheckboxes[i].checked = selectAllAdminsCheckbox.checked;
        }
    });

    function deleteAdmin() {
        var selectedIds = [];
        for (var i = 0; i < adminCheckboxes.length; i++) {
            if (adminCheckboxes[i].checked) {
                selectedIds.push(adminCheckboxes[i].value);
            }
        }
        if (selectedIds.length > 0) {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    alert(xhr.responseText);
                    location.reload();
                }
            };
            xhr.open("POST", "admin_del.jsp", true);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.send("selectedAdmin=" + selectedIds.join("&selectedAdmin="));
        } else {
            alert("请选择要删除的管理员！");
        }
    }
</script>
</body>
</html>